package io.mya.modules.customer.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.hutool.core.util.StrUtil;
import io.mya.app.utils.SHAencrypt;
import io.mya.common.utils.PageUtils;
import io.mya.common.utils.Query;
import io.mya.common.utils.R;
import io.mya.modules.customer.entity.BankCardEntity;
import io.mya.modules.customer.entity.CustomerEntity;
import io.mya.modules.customer.entity.ZtreeEntity;
import io.mya.modules.customer.service.CustomerService;
import io.mya.modules.sys.controller.AbstractController;
import io.mya.modules.sys.oauth2.TokenGenerator;

/**
 * 用户的Controller
 * 
 * @author shican
 * @email 1208296327@qq.com
 * @date 2018/03/22 14:00:00
 */
@RestController
@RequestMapping("/sys/customer")
public class CustomerController extends AbstractController{
	@Autowired
	private CustomerService customerService;
	@Autowired
	 private StringRedisTemplate stringRedisTemplate;
	/**
	 * 所有用户列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		Page<CustomerEntity> page = PageHelper.startPage(query.getPage(), query.getLimit());
		List<CustomerEntity> userList = customerService.queryList(params);
		int total= new Long(page.getTotal()).intValue();  
		
		PageUtils pageUtil = new PageUtils(userList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	/**
	 * 修改状态
	 * @param params
	 * @return
	 */
	@RequestMapping("/upuserstatus")
	public R upuserstatus(Long id,Integer type){
		CustomerEntity customerEntity = new CustomerEntity();
		customerEntity.setId(id);;
		customerEntity.setType(type);
		customerService.update(customerEntity);
		
		return R.ok();
	}
	/**
	 * 根据id查下级并且构成树形菜单
	 * @param params
	 * @return
	 */
	@RequestMapping("/selechildren")
	public R selechildren(Long id){
		//根据当前id查询其信息
		List<ZtreeEntity> list =  customerService.findchByID(id);
		return R.ok().put("list", list);
	}
	/**
	 * 根据id查询用户
	 * @param params
	 * @return
	 */
	@RequestMapping("/queryById")
	public R queryById(Long id){
		//根据当前id查询其信息
		CustomerEntity customerEntity =  customerService.findByiD(id);
		return R.ok().put("customerEntity", customerEntity);
	}
	/**
	 * 根据id查询用户银行卡信息
	 * @param params
	 * @return
	 */
	@RequestMapping("/queryBankCard")
	public R queryBankCard(Long id){
		//根据当前id查询其信息
		List<BankCardEntity> list =  customerService.queryBankCard(id);
		return R.ok().put("list", list);
	}
	/**
	 * 根据银行卡id查询用户银行卡信息
	 * @param params
	 * @return
	 */
	@RequestMapping("/queryBankCardById")
	public R queryBankCardById(Long carid){
		//根据当前id查询其信息
		BankCardEntity bankCardEntity =  customerService.queryBankCardById(carid);
		return R.ok().put("bankCardEntity", bankCardEntity);
	}
	/**
	 * 根据id修改用户的所有信息
	 * @param params
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/updateall")
	public R updateall(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String id = request.getParameter("id");//登入邮箱
		String cusname = request.getParameter("cusname");//登入邮箱
		String password = request.getParameter("password");//登入邮箱
		String candycount = request.getParameter("candycount");//登入邮箱
		CustomerEntity customerEntity =  new CustomerEntity();
		customerEntity.setId(Long.parseLong(id));
		if(StrUtil.isNotEmpty(password)){
			password = SHAencrypt.encryptSHA(password);//加密后的密码
			customerEntity.setPasswd(password);
			String token = TokenGenerator.generateValue();
			//然后把token存入redis中,修改了密码 那么就让这个帐号掉线
			stringRedisTemplate.opsForHash().put("Token",customerEntity.getId().toString(),token);
			
		}
		customerEntity.setCusname(cusname);
		customerEntity.setCandycount(Double.valueOf(candycount));
		customerService.update(customerEntity);
		String bankid = request.getParameter("bankid");//登入邮箱
		if(StrUtil.isNotEmpty(bankid)){
			String openbranch = request.getParameter("openbranch");//登入邮箱
			String peraccount = request.getParameter("peraccount");//登入邮箱
			BankCardEntity bankCardEntity = new BankCardEntity();
			bankCardEntity.setId(Long.parseLong(bankid));
			bankCardEntity.setOpenBranch(openbranch);
			bankCardEntity.setPerAccount(peraccount);
			customerService.updateBank(bankCardEntity);
		}
		
		return R.ok();
	}
	
}
